<template>
    <div style="min-width: 829px">
        <div v-if="$route.path == '/commissionCount'">
            <div class="vs-search-box">
                <el-form v-search-box-enter ref="paging" :inline="true" :model="paging" label-width="110px">
                    <el-form-item label="岗位">
                        <el-select v-model="paging.position_id" style="width: 200px" clearable>
                            <el-option v-for="item in shopList" :key="item.id" :label="item.name"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="日期：">
                        <el-date-picker v-model="bill_time_start" type="daterange" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd" start-placeholder="开始时间" end-placeholder="结束时间"
                            style="width: 300px" :clearable="false"> </el-date-picker>
                    </el-form-item>
                    <el-form-item class="vs-right-btns">
                        <el-button @click="reset">重置</el-button>
                        <el-button @click="handleCurrentChange(1)" type="primary">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="vs-table-box">
                <!-- <div class="operation-box clear-float">
                    <el-button style="margin-left:auto;">导出</el-button>
                </div> -->
                <el-table v-loading="loading" :data="list" header-row-class-name="has-gutter-table" style="width: 100%"
                    border :key="0" show-summary :summary-method="getSummaries">
                    <el-table-column type="index" label="员工序号" align="center" width="80"></el-table-column>
                    <el-table-column prop="name" label="员工姓名" align="center"></el-table-column>
                    <el-table-column prop="position" label="所属岗位" align="center"></el-table-column>
                    <el-table-column label="服务项目" align="center">
                        <el-table-column prop="project_num" label="数量" align="center"> </el-table-column>
                        <el-table-column prop="project_ach" label="业绩" align="center"></el-table-column>
                        <el-table-column prop="project_com" label="提成" align="center"></el-table-column>
                    </el-table-column>
                    <el-table-column label="成品销售" align="center">
                        <el-table-column prop="finished_num" label="数量" align="center"> </el-table-column>
                        <el-table-column prop="finished_ach" label="业绩" align="center"></el-table-column>
                        <el-table-column prop="finished_com" label="提成" align="center"></el-table-column>
                    </el-table-column>
                    <el-table-column label="毛坯销售" align="center">
                        <el-table-column prop="roughcast_num" label="数量" align="center"> </el-table-column>
                        <el-table-column prop="roughcast_ach" label="业绩" align="center"></el-table-column>
                        <el-table-column prop="roughcast_com" label="提成" align="center"></el-table-column>
                    </el-table-column>
                    <el-table-column label="洗护用品销售" align="center">
                        <el-table-column prop="wash_num" label="数量" align="center"> </el-table-column>
                        <el-table-column prop="wash_ach" label="业绩" align="center"></el-table-column>
                        <el-table-column prop="wash_com" label="提成" align="center"></el-table-column>
                    </el-table-column>
                    <el-table-column label="充值" align="center">
                        <el-table-column prop="price_num" label="数量" align="center"> </el-table-column>
                        <el-table-column prop="price_ach" label="业绩" align="center"></el-table-column>
                        <el-table-column prop="price_com" label="提成" align="center"></el-table-column>
                    </el-table-column>
                    <el-table-column prop="total_num" label="总销售额" align="center">
                        <template slot-scope="{ row }"> {{ row.total_num | toFixed2Filter }} </template>
                    </el-table-column>
                    <el-table-column prop="comTotal" label="总提成额" align="center">
                        <template slot-scope="{ row }"> {{ row.comTotal | toFixed2Filter }} </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    data() {
        return {
            bill_time_start: [],
            shopList: [],
            loading: false,
            paging: {
                position_id: '',
                page: 1,
                total: 0,
                limit: 10,
            },
            list: [],
        };
    },
    filters: {
        toFixed2Filter(val) {
            if (typeof val == 'number' || val) {
                return Number(val).toFixed(2);
            }
            return '';
        },
    },
    async created() {
        if (this.$route.path == '/commissionCount') {
            this.bill_time_start[0] = this.$moment().startOf('month').format('YYYY-MM-DD');
            this.bill_time_start[1] = this.$moment().endOf('month').format('YYYY-MM-DD');
            await this.getStaffPosition();
            this.handleCurrentChange(1);
        }
    },
    methods: {
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index == 0) {
                    sums[index] = '合计';
                    return;
                }
                const values = data.map((item) => Number(item[column.property]));
                if (column.property == 'project_ach' || column.property == 'project_com' || column.property == 'finished_ach' || column.property == 'finished_com' || column.property == 'roughcast_ach' || column.property == 'roughcast_com' || column.property == 'wash_ach' || column.property == 'wash_com' || column.property == 'price_ach' || column.property == 'price_com' || column.property == 'total_num' || column.property == 'comTotal') {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[index] = sums[index].toFixed(2);
                } else if (column.property == 'project_num' || column.property == 'finished_num' || column.property == 'roughcast_num' || column.property == 'wash_num' || column.property == 'price_num') {
                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                } else {
                    sums[index] = '';
                }
            });
            return sums;
        },
        async getStaffPosition() {
            this.loading = true;
            let { code, data } = await this.$Http.getStaffPosition({ limit: 100000 });
            this.loading = false;
            if (code) {
                this.shopList = data.data;
            }
        },
        async repairGetList() {
            if (!this.bill_time_start) {
                this.bill_time_start = [];
            }
            this.paging.date_start = this.bill_time_start[0] || '';
            this.paging.date_end = this.bill_time_start[1] || '';
            let obj = Object.assign({}, this.paging);
            this.loading = true;
            let { code, data } = await this.$Http.getCommissionSummary(obj);
            if (code) {
                this.list = data;
                // this.paging.total = data.total;
                for (let o of this.list) {
                    this.$set(o, 'comTotal', o.project_com * 1 + o.finished_com * 1 + o.roughcast_com * 1 + o.wash_com * 1 + o.price_com * 1);
                }
            }
            this.loading = false;
        },
        reset() {
            //重置
            this.paging = this.$options.data().paging;
            this.bill_time_start = [];
            this.bill_time_start[0] = this.$moment().startOf('month').format('YYYY-MM-DD');
            this.bill_time_start[1] = this.$moment().endOf('month').format('YYYY-MM-DD');
            this.handleCurrentChange(1);
        },
        handleSizeChange(val) {
            this.paging.limit = val;
            this.repairGetList();
        },
        handleCurrentChange(val) {
            this.paging.page = val;
            this.repairGetList();
        },
    },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
    width: 130px;
    height: 130px;
}

.divTable {
    width: 100%;

    div {
        border-bottom: 1px solid #ebeef5;
        border-left: 1px solid #ebeef5;
        border-right: 1px solid #ebeef5;
        height: 50px;
        line-height: 50px;
        padding-left: 30px;
    }
}

table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    table-layout: fixed;

    td {
        border: 1px solid #ebeef5;
        text-align: center;
        height: 32px;
        color: #333333;
        padding: 0 5px;
        font-size: 14px;
    }
}
</style>
